package processingdata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class FileSplitter {
	
	public static void main(String[] args)throws IOException
	{
		int totalSightings=0;
		HashMap<String,Integer> shapesStats=new HashMap<String, Integer>();
		ArrayList<String> numOfShapes=new ArrayList<String>();
		int numOfSightings=0;
		ArrayList<String> exclusions=new ArrayList<String>();
		
		
		exclusions.add("ALBERTA.CANADA");
		exclusions.add("BRITISH COLUMBIA.CAN");
		exclusions.add("MANITOBA.CANADA");
		exclusions.add("NEW BRUNSWICK.CAN");
		exclusions.add("NEWFOUNDLAND.CAN");
		exclusions.add("NOVA SCOTIA.CAN");
		exclusions.add("NORTHWEST TERRITORY");
		exclusions.add("ONTARIO.CAN");
		exclusions.add("PRINCE EDW ISLAND");
		exclusions.add("PROV OF QUE.CAN");
		exclusions.add("QC");
		exclusions.add("SASKATCHEWAN.CAN");
		exclusions.add("SK");
		exclusions.add("test");
		exclusions.add("UNSPECIFIED.INTERNATIONAL");
		exclusions.add("YUKON");
		exclusions.add("YT");
		exclusions.add("VIRGIN ISLANDS");
		exclusions.add("entries");
		
		String dir=args[0];
		ArrayList<String> files=new ArrayList<String>();
		getFilesInDirectory(dir, files);
		
		
		int counter=0;
		
		for(int i=0;i<files.size();i++)
		{
			
			
			String fileName=files.get(i);
			
			String fileNameParts[]=fileName.split("/");
			String placeName=fileNameParts[fileNameParts.length-1];
			System.out.println(fileName);
			System.out.println(placeName);
			if(exclusions.contains(placeName.trim()))
			{
				continue;
			}
			File file=new File(files.get(i));
			BufferedReader inputReader=new BufferedReader(new FileReader(file));
			
			
			while(inputReader.ready())
			{
				BufferedWriter outputWriter=new BufferedWriter(new FileWriter(new File("project3cs424/new_split_files/split_part"+counter),true));
				numOfSightings++;
				totalSightings++;
		
				if(numOfSightings==1500)
				{
					counter++;
					numOfSightings=0;
				}
				String inputLine=inputReader.readLine();
				String shape=inputLine.split("\t")[3].trim().toLowerCase();
				if(!numOfShapes.contains(shape))
				{
					numOfShapes.add(shape);
				}
				
				if(shapesStats.containsKey(shape))
				{
					int count=shapesStats.get(shape);
					count++;
					shapesStats.put(shape, count);
				}
				else
				{
					shapesStats.put(shape, 1);
				}
				
				
				outputWriter.write(inputLine+"\n");
				
				outputWriter.close();					
				
			}
			
		}
		System.out.println(numOfShapes.size());
		System.out.println(totalSightings);
		for(int i=0;i<numOfShapes.size();i++)
		{
			System.out.println(numOfShapes.get(i));
		}
		
		Set<String> shapesKey=shapesStats.keySet();
		Iterator<String> shapesKeyIterator=shapesKey.iterator();
		while(shapesKeyIterator.hasNext())
		{
			String shapes=shapesKeyIterator.next();
			System.out.println(shapes+" "+shapesStats.get(shapes));
		}
	}
		
	public static void getFilesInDirectory(String dirName,ArrayList<String> files)
	{
		File dir=new File(dirName);
		
		//System.out.println(dir);
		if(dir.isDirectory())
		{
			if(dir.getName().contains(".svn"))
			{
				//do nothing
			}
			else
			{
			
				File[] fileArray=dir.listFiles();
							
				for(int i=0;i<fileArray.length;i++)
				{
				//	System.out.println(fileArray[i]);
					getFilesInDirectory(fileArray[i].getAbsolutePath(),files);
				}

			}
					}
		else if(dir.isFile())
		{
			files.add(dir.getAbsolutePath());
		}
		
		
	}

}
